package com.hanlink.dsp.ui.widget.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.hanlink.dsp.util.DrawFunction;
import com.hanlink.dsp.util.EqUtil;

public class EqView extends EqGridView {

    public static byte ch = 0;
    private static Paint paintDot = new Paint();
    private static Paint paintGray = new Paint();
    private static Paint paintLRX = new Paint();
    private static Paint paintRed = null;
//    protected static final int rectR = 20;
//    public boolean DrawLRX = false;
    protected boolean[] boolean_peq = null;
    protected boolean[] boolean_xover = null;
    private int drawI = 0;
    private float[] fline_all = null;
    private float[][] fline_peq = null;
    private float[][] fline_xover = null;
    private double[] line_all = null;
    private double[][] line_peq = null;
    private double[][] line_xover = null;
    protected int[] mGain = null;
    protected int[] mFreq = null;
    protected int[] mXover = null;
    protected byte[] mXoverType;
    protected float[] mQ = null;
    protected int peqLength = 0;
    protected RectF[] rect_peq = null;
    protected RectF[] rect_xover = null;


    private static int[] eqColors = {Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW, Color.WHITE, Color.rgb(0xa0, 0x20, 0xf0), Color.rgb(0x8e, 0x8e, 0x38)};

    public EqView(Context context) {
        super(context);
        InitPaint();
    }

    public EqView(Context context, AttributeSet attrs) {
        super(context, attrs);
        InitPaint();
    }

    public EqView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        InitPaint();
    }


    private void InitPaint() {
        if (paintRed == null) {
            paintRed = new Paint();
            paintRed.setColor(Color.RED);
            paintRed.setStrokeWidth(3.0F);
            paintGray.setColor(Color.GRAY);
            paintGray.setStrokeWidth(2.0F);
            paintDot.setColor(baseColor);
            paintDot.setTextSize(24.0F);
            paintDot.setStrokeWidth(2.0F);
            paintLRX.setColor(-1);
            paintLRX.setStrokeWidth(10.0F);
            paintLRX.setTextSize(60.0F);
            paintLRX.setAlpha(187);
        }
    }

    public void setColor(int index) {
        paintRed.setColor(eqColors[index]);
    }

    private void colOverlay() {
        if (this.line_all != null) {
            for (int idx = 0; idx < this.line_all.length; ++idx) {
                this.line_all[idx] = 0.0D;
            }
            double[] tmpArray = this.line_all;
            for (int index = 0; index < this.peqLength; ++index) {
                for (int j = 0; j < this.line_all.length; ++j) {
                    if (this.line_peq != null && this.line_peq.length > index && this.line_peq[index] != null && this.line_peq[index].length > j) {
                        tmpArray[j] += this.line_peq[index][j];
                    }
                }
            }
            if (mXover != null) {
                double[] var5;
                for (int i = 0; i < this.mXover.length; ++i) {
                    for (int j = 0; j < this.line_all.length; ++j) {
                        var5 = this.line_all;
                        var5[j] += this.line_xover[i][j];
                    }
                }
            }
            EqUtil.double2float(this.times, this.mTop + (this.mBottom - this.mTop) / 2, this.line_all, 60, this.fline_all);
        }
    }

    private void colPEQ(int width) {
        if (this.line_peq != null) {
            for (int index = 0; index < this.peqLength; ++index) {
                this.colPEQ(index, width);
            }
        }
    }

    private void colPEQ(int eqIndex, int width) {
        if (this.line_peq[eqIndex] != null && this.line_peq.length > 0) {
            int height = this.mBottom - this.mTop;
            EqUtil.cal_point_peq(mQ[eqIndex], this.mGain[eqIndex], mFreq[eqIndex], this.line_peq[eqIndex]);
            EqUtil.double2float(this.times, this.mTop + height / 2, this.line_peq[eqIndex], 60, this.fline_peq[eqIndex]);
            EqUtil.cal_rect(this.rect_peq[eqIndex], 20, this.mGain[eqIndex], mFreq[eqIndex], width - 60 - 20, height, 60, this.mTop, 18);
        }
    }


    private void drawLines(Canvas canvas) {
        if (this.mGain != null) {
            if (this.drawI >= this.peqLength && this.drawI - this.peqLength > this.boolean_xover.length - 1) {
                this.drawI = 0;
            }
            int var4;
            if (this.drawI < this.peqLength) {
                if (this.boolean_peq[this.drawI]) {
                    canvas.drawLines(this.fline_peq[this.drawI], paintGray);
                }
            } else {
                var4 = this.drawI - this.peqLength;
                if (this.boolean_xover[var4]) {
                    canvas.drawLines(this.fline_xover[var4], paintGray);
                }
            }
            canvas.drawLines(this.fline_all, paintRed);
        }

    }

    private boolean reArray(int w) {
        boolean rst = true;
        if (this.line_peq != null && w >= 1) {
            int width = w - 60 - 20;
            this.line_all = new double[width];
            this.fline_all = new float[width * 4];

            for (w = 0; w < this.line_peq.length; ++w) {
                this.line_peq[w] = new double[width];
                this.fline_peq[w] = new float[width * 4];
            }

            if (boolean_xover != null && boolean_xover[0]) {
                for (int i = 0; i < this.line_xover.length; i++) {
                    this.line_xover[i] = new double[width];
                    this.fline_xover[i] = new float[width * 4];
                }
            }

        } else {
            rst = false;
        }
        return rst;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.drawLines(canvas);
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldw, int oldh) {
        super.onSizeChanged(width, height, oldw, oldh);
        this.times = EQ_TIMES_CAL_P * (float) (this.mBottom - this.mTop);
//        this.times = -0.765556F * (float) (this.mBottom - this.mTop);
        this.reArray(width);
        this.colPEQ(width);
        this.colXover(width);
        this.colOverlay();
    }

    public void reArray() {
        if (this.reArray(this.getWidth())) {
            this.refreshPEQ();
            this.refreshXover();
        }

    }

    public void refreshPEQ() {
        for (int var1 = 0; var1 < this.peqLength; ++var1) {
            this.colPEQ(var1, this.getWidth());
        }
        this.colOverlay();
        this.invalidate();
    }

    public void refreshPEQ(int index) {
        this.drawI = index;
        this.colPEQ(index, this.getWidth());
        this.colOverlay();
        this.invalidate();
    }


    public void resetPEQ(float[] q, int[] eq, int[] freq) {
        this.mGain = eq;
        this.mQ = q;
        this.mFreq = freq;
        refreshPEQ();
    }

    private void colXover(int width) {
        if (this.line_xover != null) {
            for (int i = 0; i < this.mXover.length; ++i) {
                this.colXover(i, width);
            }
        }

    }

    private void colXover(int xoverIndex, int width) {
        if (this.line_xover[xoverIndex] == null) {
            return;
        }
        int height = this.mBottom - this.mTop;
        int freq = mXover[xoverIndex];
        byte type = mXoverType[xoverIndex];
        boolean var6;
        if (xoverIndex == 1) {
            var6 = true;
        } else {
            var6 = false;
        }

        DrawFunction.cal_point_xover(freq, type, var6, this.line_xover[xoverIndex], false);
        EqUtil.double2float(this.times, this.mTop + height / 2, this.line_xover[xoverIndex], 60, this.fline_xover[xoverIndex]);
        EqUtil.cal_rect(this.rect_xover[xoverIndex], 20, 0, freq, width - 60 - 20, height, 60, this.mTop, 18);
    }

    public void refreshXover() {
        if (mXover != null && mXover.length > 0) {
            for (int i = 0; i < this.mXover.length; ++i) {
                this.colXover(i, this.getWidth());
            }
        }

        this.colOverlay();
        this.invalidate();
    }

    public void refreshXover(int var1) {
        this.drawI = this.mGain.length + var1;
        this.colXover(var1, this.getWidth());
        this.colOverlay();
        this.invalidate();
    }

    public void setPEQ(float[] q, int[] gain, int[] freq) {
        this.mGain = gain;
        this.mQ = q;
        this.mFreq = freq;
        this.peqLength = gain.length;
        this.line_peq = new double[gain.length][];
        this.fline_peq = new float[gain.length][];
        this.boolean_peq = new boolean[gain.length];
        this.rect_peq = new RectF[gain.length];

        for (int var2 = 0; var2 < this.boolean_peq.length; ++var2) {
            this.boolean_peq[var2] = true;
            this.rect_peq[var2] = new RectF(0.0F, 0.0F, 40.0F, 40.0F);
        }
    }

    public void resetXover(int[] xover, byte[] xoverType) {
        if (line_xover == null) {
            setXover(xover, xoverType);
            reArray();
            return;
        }
        this.mXover = xover;
        this.mXoverType = xoverType;
        refreshXover();
    }

    public void setXover(int[] xover, byte[] xoverType) {
        this.mXover = xover;
        this.mXoverType = xoverType;
        this.line_xover = new double[xover.length][];
        this.fline_xover = new float[xover.length][];
        this.boolean_xover = new boolean[xover.length];
        this.rect_xover = new RectF[xover.length];
        for (int i = 0; i < this.boolean_xover.length; i++) {
            this.boolean_xover[i] = true;
            this.rect_xover[i] = new RectF(0.0F, 0.0F, 40.0F, 40.0F);
        }
    }

    public void setPEQDisplay(int var1, boolean var2) {
        this.boolean_peq[var1] = var2;
    }

    public void setPEQDisplay(boolean var1) {
        for (int var2 = 0; var2 < this.boolean_peq.length; ++var2) {
            this.boolean_peq[var2] = var1;
        }

    }


    public void setXoverDisplay(int var1, boolean var2) {
        this.boolean_xover[var1] = var2;
    }

    public void setXoverDisplay(boolean var1) {
        this.boolean_xover[1] = var1;
        this.boolean_xover[0] = var1;
    }

}
